from BasicLibraries import *

    


def make_frame(golden, initiative_set, STATS=None, sectors = 'all'):
    res = pd.DataFrame()
    if sectors != 'all':
        bd = golden[golden.GICS_level_1.isin(sectors)].copy()
    else:
        bd = golden.copy()
    if initiative_set != 'ALL':
        bd['initiatives'] = bd[initiative_set].sum(axis = 1)
        bd['initiatives_tot'] = bd['number_of_initiatives']
    else:
        bd['initiatives'] = bd['number_of_initiatives']
        bd['initiatives_tot'] = bd['number_of_initiatives']
    if STATS==None:
        bd = bd[['initiatives', 'initiatives_tot',   'rfyear', 'ids', 'gvkey']]
    else:
        bd = bd[['initiatives', 'initiatives_tot', STATS,  'rfyear', 'ids', 'gvkey']]

    bd['mrg'] = bd['rfyear'].astype(int).astype(str)+'-'+bd['ids']
    return bd

def make_strategy(golden, strategy_type, scaler_type):
    bd = golden.copy()

    if scaler_type == 'relative':
        scaler_I = ['communication', 'donation & funding', 'association', 'pricing',
                       'adoption of standards and rules', 'assessment and measurement',
                       'organizational structuring', 'modification of procedures',
                       'asset modification', 'training', 'incentives', 'r&d investments',
                       'new products', 'volunteerism']
        scaler_ = bd[scaler_I].sum(axis = 1)

    elif scaler_type == 'total':
        scaler_ = 1
    if strategy_type == 'Innovation':
        target_initiatives=  ['r&d investments', 'new products', 'association', 'organizational structuring'] 
        bd['Strategy'] = bd[target_initiatives].sum(axis = 1)/scaler_
    elif strategy_type == 'Risk mitigation':
        target_initiatives =  ['modification of procedures', 'asset modification', 'training',  'assessment and measurement', 'adoption of standards and rules']
        bd['Strategy'] = bd[target_initiatives].sum(axis = 1)/scaler_
    elif strategy_type == 'Stakeholders engagement':
        target_initiatives =  ['donation & funding', 'communication', 'volunteerism', 'incentives', 'pricing']
        bd['Strategy'] = bd[target_initiatives].sum(axis = 1)/scaler_
    elif strategy_type == 'Concentration':
        target_initiatives = None
        bd['Strategy'] = bd['concentration']    
    elif strategy_type == 'Entropy':
        target_initiatives = None
        bd['Strategy'] = bd['Entropy']
    elif strategy_type == 'EntropyOLD':
        target_initiatives = None
        bd['Strategy'] = bd['EntropyOLD']
    elif strategy_type == 'Simpson':
        target_initiatives = None
        bd['Strategy'] = bd['Simpson']
    else: print('Error! Declare an existing strategy')


    bd['initiatives_tot'] = bd['number_of_initiatives']
    bd = bd[['Strategy', 'concentration', 'initiatives_tot', 'rfyear', 'ids', 'gvkey', 'GICS_level_1']].rename(columns = {'GICS_level_1': 'Sectors'})

    bd['mrg'] = bd['rfyear'].astype(int).astype(str)+'-'+bd['ids']    
    bd['Strategy'] = bd['Strategy'].replace([-np.inf, np.inf], [0,0])
    return bd, target_initiatives


